package com.eascs.ant.user.interceptor;

import javax.inject.Inject;

import org.springframework.stereotype.Component;

import com.eascs.ant.common.entity.StatusConstant;
import com.eascs.ant.user.service.UserService;
import com.eascs.web.sso.MenuItem;
import com.eascs.web.sso.SsoUser;
import com.eascs.web.sso.SsoUserFilter;
import com.eascs.web.sso.User;

@Component
public class TicketSsoUserFilter implements SsoUserFilter {

	@Inject
	private UserService userService;

	@Override
	public void filter(SsoUser ssoUser) {
		User user = ssoUser.getUser();
		if (null == user)
			return;
		if ("muwang.zheng".equals(user.getAccount()) || "jun.lu".equals(user.getAccount()) || "hongquan.tang".equals(user.getAccount())) {
			user.setSuperAdministrator(true);
		}
		user.getMenuItemTree().clear();
		user.getPermissionCodeMap().clear();
		user.getPermissionUrlMap().clear();
		com.eascs.ant.user.entity.User userEntity = new com.eascs.ant.user.entity.User();
		userEntity.setCode(user.getCode());
		userEntity.setLoginName(user.getAccount());
		userEntity.setName(user.getCname());
		userEntity.setEmail(user.getEmail());
		userEntity.setPhone(user.getPhone());
		userEntity.setStatus(StatusConstant.STATUS_OK);
		userService.saveUser(userEntity);

		buildMenu(user);

	}

	private void buildMenu(User user) {
		MenuItem ticketMenuItem = new MenuItem();
		user.getMenuItemTree().add(ticketMenuItem);
		ticketMenuItem.setName("首页");
		ticketMenuItem.setUrl("/");
		ticketMenuItem.setCode("ticketMenuItem");

	}


}
